package cn.tedu.store.product.webapi.controller;

import cn.tedu.store.commons.pojo.product.dto.CategoryAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.CategoryUpdateDTO;
import cn.tedu.store.commons.pojo.product.vo.CategoryListItemVO;
import cn.tedu.store.commons.pojo.product.vo.CategoryStandardVO;
import cn.tedu.store.commons.web.JsonPage;
import cn.tedu.store.product.service.ICategoryService;
import cn.tedu.store.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "05.分类管理模块")
@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理[添加分类]请求 参数:{}",categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理[删除分类]请求 参数:{}",id);
        categoryService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("启用分类")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id){
        log.debug("开始处理[启用分类]请求 参数:{}",id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用分类")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id){
        log.debug("开始处理[禁用分类]请求 参数:{}",id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("显示分类")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/display")
    public JsonResult<Void> setDisplay(@PathVariable Long id){
        log.debug("开始处理[显示分类]请求 参数:{}",id);
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    @ApiOperation("隐藏分类")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/hidden")
    public JsonResult<Void> setHidden(@PathVariable Long id){
        log.debug("开始处理[隐藏分类]请求 参数:{}",id);
        categoryService.setHidden(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改分类信息")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/updateById")
    public JsonResult<Void> updateById(@PathVariable Long id, CategoryUpdateDTO categoryUpdateDTO){
        log.debug("开始处理[修改分类信息]请求 参数:{}",id);
        categoryService.updateById(id,categoryUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("获取分类详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/get-by-id")
    public JsonResult<CategoryStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理[获取分类详情]请求 参数:{}",id);
        return JsonResult.ok(categoryService.getStandardById(id));
    }

    @ApiOperation("根据父类id获取分类列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "parentId", value = "父类id", required = true, dataType = "long")
    })
    @GetMapping("/list-by-parentId")
    public JsonResult<JsonPage<CategoryListItemVO>> listByParentId(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) Long parentId
    ){
        log.debug("开始处理[获取子类分类列表]请求");
        return JsonResult.ok(categoryService.listByParentId(parentId, pageNum, pageSize));
    }

    @ApiOperation("获取分类列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int")
    })
    @GetMapping("")
    public JsonResult<JsonPage<CategoryListItemVO>> listByParentId(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ){
        log.debug("开始处理[获取分类列表]请求 参数:无");
        return JsonResult.ok(categoryService.list(pageNum, pageSize));
    }

}
